package com.tanhua.backstage.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.tanhua.backstage.vo.PageResult;
import com.tanhua.common.mapper.UserInfoMapper;
import com.tanhua.dubbo.server.api.LogApi;
import com.tanhua.dubbo.server.api.QuanZiApi;
import com.tanhua.dubbo.server.pojo.UserLogin;
import com.tanhua.dubbo.server.vo.LogRocordVo;
import com.tanhua.dubbo.server.vo.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class LogService {
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Reference(version = "1.0.0")
    private QuanZiApi quanZiApi;
    @Reference(version = "1.0.0")
    private LogApi logApi;


    public PageResult getLogRecordPage(Integer page, Integer pageSize, String sortProp, String sortOrder, Integer type, Long uid) {
        //查询日志信息
        PageInfo<UserLogin> pageInfo = logApi.findLogRecord(page, pageSize, sortProp, sortOrder, type, uid);
        //获取日志集合
        List<UserLogin> userLoginList = pageInfo.getRecords();
        //创建返回前端的数据集合
        List<LogRocordVo> logRocordVoList = new ArrayList<>();
        //遍历日志集合
        for (UserLogin userLogin : userLoginList) {
            LogRocordVo logRocordVo = new LogRocordVo();
            logRocordVo.setId(userLogin.getUserId());//用户id
            logRocordVo.setLogTime(userLogin.getLogTime());//登入时间
            logRocordVo.setPlace(userLogin.getPlace());//登入地点
            logRocordVo.setEquipment(userLogin.getEquipment());//登入设备
            //添加到返回前端的数据集合中
            logRocordVoList.add(logRocordVo);
        }
        //封装pageResult
        PageResult pageResult = new PageResult();
        pageResult.setPage(page); //当前页
        pageResult.setPagesize(pageSize);//每页显示条数
        pageResult.setItems(logRocordVoList);//返回的给前端的日志集合
        pageResult.setCounts(pageInfo.getTotal());//总条数

        //总页数
        if(pageInfo.getTotal()%pageSize==0){
            pageResult.setPages(pageInfo.getTotal()/pageSize);
        } else {
            pageResult.setPages(pageInfo.getTotal()/pageSize+1);
        }

        return pageResult;
    }



}
